<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends CI_Model {
    
    public $tb_category = 'category';
    public $tb_category_desc = 'category_desc';
    public $tb_link = 'link';
    public $tb_image = 'image';
    
    function __construct() {
        parent::__construct();
        $this->load->library('session');
    }
    
    /* *****************************************************************************************************************
     * Name         : max_sort_order
     * -----------------------------------------------------------------------------------------------------------------
     * Description  :
     * -----------------------------------------------------------------------------------------------------------------
     * Params       : 
     * -----------------------------------------------------------------------------------------------------------------
     * Return       :
     * -----------------------------------------------------------------------------------------------------------------
     * Warning      :
     * -----------------------------------------------------------------------------------------------------------------
     * Copyright    : TAOWEBSITE
     * -----------------------------------------------------------------------------------------------------------------
     * Created      : New  2014/07/20 (TienPham)  
     * *************************************************************************************************************** */
    
    public function max_sort_order()
    {
        $dbprefix = $this->config->item('dbprefix');
        $sql = "select max(sort_order) as max_sort_order from ". $dbprefix . $this->tb_category;
        $data = $this->db->query($sql)->row_array();
        return $data['max_sort_order']+1;
    }
    
    public function get_all_category()
    {
        $lang_default = $this->session->userdata('lang_default');
        $lang_current = $this->session->userdata('lang_current');
        
        $dbprefix = $this->config->item('dbprefix');
        $sql = "SELECT * 
                FROM ". $dbprefix.$this->tb_category ." c
                LEFT JOIN ". $dbprefix.$this->tb_category_desc ." cd on c.category_id = cd.category_id and cd.lang_code = '{$lang_default['lang_code']}'
                ORDER BY c.sort_order";
        $sql1 = "SELECT * 
                FROM ". $dbprefix.$this->tb_category ." c
                LEFT JOIN ". $dbprefix.$this->tb_category_desc ." cd on c.category_id = cd.category_id and cd.lang_code = '{$lang_current['lang_code']}'
                ORDER BY c.sort_order";
                
        $data['default'] = $this->db->query($sql)->result_array();
        $data['current'] = $this->db->query($sql1)->result_array();
       
        if(!empty($data['default'])){
            $data['current'] = replaceValueNull($data['current'], $data['default']);
        }
        $data['current'] = @$data['current'];
        $data['default'] = @$data['default'];
        return $data;
    }
    
    public function insert($data)
    {
        $dbprefix = $this->config->item('dbprefix');
        
        /*INSERT CATEGORY*/
        $category = $data['category'];
        $this->db->insert($dbprefix.$this->tb_category, $category);
        $category_id = $this->db->insert_id();
        
        /*INSERT CATEGORY_DESC*/
        $category_desc = $data['category_desc'];
        foreach($category_desc as $lang_code => $value)
        {
            $cate_desc = array();
            $cate_desc = $value;
            $cate_desc['category_id'] = $category_id;
            $cate_desc['lang_code'] = $lang_code;
            
            $this->db->insert($dbprefix.$this->tb_category_desc, $cate_desc);
        }
        
        /*INSERT LINK*/
        $link = $data['link'];
        foreach($link as $lang_code => $value)
        {
            $lin = array();
            $lin = $value;
            $lin['category_id'] = $category_id;
            $lin['type'] = 'category';
            $lin['lang_code'] = $lang_code;
            
            $this->db->insert($dbprefix.$this->tb_link, $lin);
        }
        
        /*INSERT IMAGE*/
        $image = array();
        
        $main_image = $data['image']['main_image'];
            $arr_url_img = explode('public/upload/images/', $main_image);
            $name_img = $arr_url_img[1];
            $url = 'public/upload/images/' . $name_img;
        $image['post_id'] = $category_id;
        $image['url'] = $url;
        $image['type'] = 'category';
        $image['status'] = '1';
        $this->db->insert($dbprefix.$this->tb_image, $image);
        
        $other_image = $data['image']['other_image'];
        $order_image = $data['image']['order_image'];
        foreach($other_image as $key => $url_img)
        {
            $arr_url_img = explode('public/upload/images/', $url_img);
            $name_img = $arr_url_img[1];
            
            if($name_img != 'no-image.jpg')
            {
                $url = 'public/upload/images/' . $name_img;
                $image['post_id'] = $category_id;
                $image['url'] = $url;
                $image['type'] = 'category';
                $image['status'] = '0';
                $image['sort_order'] = $order_image[$key];
                $this->db->insert($dbprefix.$this->tb_image, $image);
            }
        }
        return 1;
    }
    
    public function update($category_id, $data)
    {
        $dbprefix = $this->config->item('dbprefix');
        
        /*INSERT CATEGORY*/
        $category = $data['category'];
        $this->db->where('category_id', $category_id);
        $this->db->update($dbprefix.$this->tb_category, $category);

        
        /*INSERT CATEGORY_DESC*/
        $category_desc = $data['category_desc'];
        foreach($category_desc as $lang_code => $value)
        {
            $cate_desc = array();
            $cate_desc = $value;
            $cate_desc['lang_code'] = $lang_code;
            
            $sql = "SELECT 1 FROM ". $dbprefix.$this->tb_category_desc ." WHERE `category_id` = '$category_id' AND `lang_code` = '$lang_code'";
            $check = $this->db->query($sql)->row_array();
            if(!empty($check))
            {
                $this->db->where('category_id', $category_id);
                $this->db->where('lang_code', $lang_code);
                $this->db->update($dbprefix.$this->tb_category_desc, $cate_desc);
            }
            else
            {
                $cate_desc['category_id'] = $category_id;
                $this->db->insert($dbprefix.$this->tb_category_desc, $cate_desc);
            }
        }
        
        /*INSERT LINK*/
        $link = $data['link'];
        foreach($link as $lang_code => $value)
        {
            $lin = array();
            $lin = $value;
            
            $lin['type'] = 'category';
            $lin['lang_code'] = $lang_code;
            
            $sql = "SELECT 1 FROM ". $dbprefix.$this->tb_link ." WHERE `category_id` = '$category_id' AND `lang_code` = '$lang_code'";
            $check = $this->db->query($sql)->row_array();
            if(!empty($check))
            {
                $this->db->where('category_id', $category_id);
                $this->db->where('type', 'category');
                $this->db->where('lang_code', $lang_code);
                $this->db->update($dbprefix.$this->tb_link, $lin);
            }
            else
            {
                $lin['category_id'] = $category_id;
                $this->db->insert($dbprefix.$this->tb_link, $lin);
            }
        }
        
        /*INSERT IMAGE*/
        $this->db->where('post_id', $category_id);
        $this->db->delete($dbprefix.$this->tb_image);
        
        $image = array();
        $main_image = $data['image']['main_image'];
            $arr_url_img = explode('public/upload/images/', $main_image);
            $name_img = $arr_url_img[1];
            $url = 'public/upload/images/' . $name_img;
        $image['post_id'] = $category_id;
        $image['url'] = $url;
        $image['type'] = 'category';
        $image['status'] = '1';
        $this->db->insert($dbprefix.$this->tb_image, $image);
        
        $other_image = $data['image']['other_image'];
        $order_image = $data['image']['order_image'];
        foreach($other_image as $key => $url_img)
        {
            $arr_url_img = explode('public/upload/images/', $url_img);
            $name_img = $arr_url_img[1];
            
            if($name_img != 'no-image.jpg')
            {
                $url = 'public/upload/images/' . $name_img;
                $image['post_id'] = $category_id;
                $image['url'] = $url;
                $image['type'] = 'category';
                $image['status'] = '0';
                $image['sort_order'] = $order_image[$key];
                $this->db->insert($dbprefix.$this->tb_image, $image);
            }
        }
        return 1;
    }
    
    public function get_category_by_id($category_id = "")
    {
        $lang_default = $this->session->userdata('lang_default');
        $lang_current = $this->session->userdata('lang_current');
        
        $dbprefix = $this->config->item('dbprefix');
        
        $sql = "select c.* 
                from ". $dbprefix.$this->tb_category ." c
                where c.category_id = '$category_id'";
        $data['category'] = $this->db->query($sql)->row_array();
        
        $sql = "select * from ". $dbprefix.$this->tb_category_desc ." where category_id = '$category_id'";
        $rs = $this->db->query($sql)->result_array();
        $category_desc = array();
        foreach($rs as $cate)
        {
            $category_desc[$cate['lang_code']] = $cate;
        }
        $data['category_desc'] = $category_desc;
        
        $sql = "SELECT * FROM ". $dbprefix.$this->tb_link ." WHERE `type` = 'category' AND category_id = '$category_id'";
        $rs = $this->db->query($sql)->result_array();
        $link = array();
        foreach($rs as $l)
        {
            $link[$l['lang_code']] = $l;
        }
        $data['link'] = $link;
        return $data;
    }
    
    public function get_image_category_by_id($category_id = "")
    {
        $dbprefix = $this->config->item('dbprefix');
        $sql = "SELECT * FROM ". $dbprefix.$this->tb_image ." WHERE post_id = '$category_id' AND type = 'category' ORDER BY sort_order";
        $data = $this->db->query($sql)->result_array();
        $i = 0;
        $image = array();
        foreach($data as $value)
        {
            if($value['status'] == 1)
            {
                $image['main_image']['url'] = $value['url'];
                $image['main_image']['thumb'] = str_replace('public/upload/images/', 'public/upload/_thumbs/images/', $value['url']);
            }
            else
            {
                $image['other_image'][$i]['url'] = $value['url'];
                $image['other_image'][$i]['thumb'] = str_replace('public/upload/images/', 'public/upload/_thumbs/images/', $value['url']);
                $image['other_image'][$i]['sort_order'] = $value['sort_order'];
                $i++;
            }
        }
        return $image;
    }
    

    public function list_category($arrData, $parent = 0, $level = 0, &$result = array()) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parent == $val['parent_id']) {
                    $val['level'] = $level;
                    $result[] = $val;
                    $_parent = $val['category_id'];
                    unset($arrData[$key]);
                    $this->list_category($arrData, $_parent, $level + 1, $result);
                }
            }
        }
        return $result;
    }
    
    function change_status($category_id = "")
    {
        $dbprefix = $this->config->item('dbprefix');
        $sql = "SELECT * FROM ". $dbprefix.$this->tb_category ." WHERE `category_id` = '$category_id'";
        $data = $this->db->query($sql)->row_array();
        if(!empty($data))
        {
            $status = $data['status'];
            $change = $status == 1 ? 0 : 1;
            $sql = "UPDATE ". $dbprefix.$this->tb_category ." SET `status` = '$change' WHERE `category_id` = '$category_id'";
            $this->db->query($sql);
            return $change;
        }
        else
            return -1;
    }
    
    function delete_category($category_id = "")
    {
        $dbprefix = $this->config->item('dbprefix');
        
        $this->db->where('category_id', $category_id);
        $this->db->delete($dbprefix.$this->tb_category);
        
        $this->db->where('category_id', $category_id);
        $this->db->delete($dbprefix.$this->tb_category_desc);
        
        $this->db->where('post_id', $category_id);
        $this->db->where('type', 'category');
        $this->db->delete($dbprefix.$this->tb_image);
    }

}
